package indi.h.devmg.acquisition.snmp.mib.handles;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;

import indi.h.devmg.acquisition.snmp.mib.beans.MIB;
import indi.h.devmg.acquisition.snmp.mib.beans.Node;
import indi.h.devmg.acquisition.snmp.mib.configs.OIDConfig;
import indi.h.devmg.acquisition.snmp.mib.util.ArrayFormat;
import indi.h.devmg.handle.Handle;

public class MIBHandle implements Handle<MIB, Boolean> {

	@Override
	public Boolean handle(MIB t) {
		// TODO Auto-generated method stub
		Map<String, Node> nodes = t.getNodes();
		t.setOidConfig(createOid(nodes));
		return true;
	}

	protected OIDConfig createOid(Map<String, Node> m) {
		Map<String, String[]> data = new HashMap<String, String[]>();
		OIDConfig oc = new OIDConfig();
		for (Entry<String, Node> k : m.entrySet()) {
			Node value = k.getValue();
			searchOid(value, m, data);
			//是否对大小写敏感
			//String poid = value.getPoid().toLowerCase();
			String poid = value.getPoid();
			if (poid != null) {
				if (oc.contain(poid)){
					//是否对大小写敏感
					//oc.putOid(value.getName().toLowerCase(), oc.getOid(poid) + value.getOid());
					oc.putOid(value.getName(), oc.getOid(poid) + value.getOid());
				}
			}
		}
		return oc;
	}

	protected String[] searchOid(Node n, Map<String, Node> m, Map<String, String[]> data) {
		String[] result = new String[2];
		//是否对大小写敏感
		//String lowerCase2 = n.getName().toLowerCase();
		String lowerCase2 = n.getName();
		if (data.containsKey(lowerCase2)) {
			String[] createOid = data.get(lowerCase2);
			result = createOid;
			n.setOid(createOid[0]);
			n.setPoid(createOid[1]);
			return result;
		}
		String oid = "";
		String parent = n.getParent().replaceAll("\\[|\\]", "");
		List<String> removeEmpty = ArrayFormat.removeEmpty(parent.split(","));
		for (int i = removeEmpty.size() - 1; i >= 0; i--) {
			String string = removeEmpty.get(i);
			if (string.matches("[0-9]+")) {
				oid = "." + string + oid;
			} else if (string.indexOf("(") >= 0 && string.indexOf(")") > 0) {
				String substring = string.substring(string.indexOf("(") + 1, string.indexOf(")"));
				oid = "." + substring + oid;
			} else {
				//是否对大小写敏感
				//String lowerCase = string.toLowerCase();
				String lowerCase = string;
				if (m.containsKey(lowerCase)) {
					String[] createOid = searchOid(m.get(lowerCase), m, data);
					oid = createOid[0] + oid;
					result[1] = createOid[1];
					n.setPoid(createOid[1]);
				} else {
					result[1] = lowerCase;
					n.setPoid(lowerCase);
				}
			}
		}
		n.setOid(oid);
		result[0] = oid;
		//是否对大小写敏感
		//data.put(n.getName().toLowerCase(), result);
		data.put(n.getName(), result);
		return result;
	}

	public static void main(String[] args) {
		String parent = "[mib-2, 27]".replaceAll("\\[|\\]", "");
		List<String> removeEmpty = ArrayFormat.removeEmpty(parent.split(","));
		String oid = "";
		for (int i = removeEmpty.size() - 1; i >= 0; i--) {
			String string = removeEmpty.get(i);
			if (string.matches("[0-9]+")) {
				oid = "." + string + oid;
			} else if (string.indexOf("(") >= 0 && string.indexOf(")") > 0) {
				String substring = string.substring(string.indexOf("(") + 1, string.indexOf(")"));
				oid = "." + substring + oid;
			} else {
				oid = "." + string + oid;
			}
		}
		System.out.println(oid);
		String[] a = new String[20];
		System.out.println(a[10]);
	}

}
